package com.hdoit.kylin.web.money.controllers;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.money.model.Reimbursement;
import com.hdoit.kylin.logical.money.service.IReimbursementService;
import com.hdoit.kylin.logical.relative.service.IProjectUserService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.money.ReimbursementSearch;
import com.hdoit.kylin.selfcommon.enums.RoleMapper;
import com.hdoit.kylin.web.common.CommonWebUtils;

/**
*@author liyaochong
*@email
*@createtime 2017年07月19日 09:44:13
*@comment
*/
@Controller
@RequestMapping(value="/admin/money/reimbursement/")
public class ReimbursementController extends BaseController{
	@Override
	public String getRoutePath(){
		return "areas/admin/money/reimbursement/";
	}
	
	private IReimbursementService reimbursementService;
	public IReimbursementService getReimbursementService(){
		return reimbursementService;
	}
	@Resource
	public void setReimbursementService(IReimbursementService reimbursementService) {
		this.reimbursementService=reimbursementService;
	}	
	
	private IProjectUserService project_userService;
	public IProjectUserService getProject_userService(){
		return project_userService;
	}
	@Resource
	public void setProject_userService(IProjectUserService project_userService) {
		this.project_userService=project_userService;
	}
	
	private IStudentService studentService;
	public IStudentService getStudentService(){
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService=studentService;
	}
	
	private ITeacherService teacherService;
	public ITeacherService getTeacherService(){
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService=teacherService;
	}
	
	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String detailedViewName="reimbursementDetailed";
	private final String saveViewName="save";
	private final String deleteViewName= "delete";
	
	@RequestMapping(value=indexViewName,method= {RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,PagerModel pagerModel,ReimbursementSearch search,boolean ajaxPost,
			HttpServletRequest request){
		if(CommonWebUtils.isTeacher(getLoginModel())){
			List<String> projectIds=project_userService.queryList(getLoginModel().getLoginId());
			search.setProjectIds(projectIds);
			search.setUserType(RoleMapper.teacher);
		}
		else if(CommonWebUtils.isStudent(getLoginModel())){
			List<String> projectIds=project_userService.queryList(getLoginModel().getLoginId());
			search.setProjectIds(projectIds);
			search.setUserType(RoleMapper.undergraduate);
		}
		else if(CommonWebUtils.isOperation(getLoginModel())){
			search.setUserType(RoleMapper.operation);
		}
		else if(CommonWebUtils.isManager(getLoginModel())){
			search.setUserType(RoleMapper.manager);
		}
		pagerModel.setTotalAmount(reimbursementService.queryCount(search));	
		List<Reimbursement> reimbursements= reimbursementService.queryListByProjectId(search,pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("reimbursements", reimbursements);
		model.addAttribute("reimbursementSearch", search);
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=detailedViewName,method ={RequestMethod.GET})
	public String reimbursementDetailed(Model model,String key){
		Reimbursement reimbursement=new Reimbursement();
		if(!StringUtils.isEmpty(key)){
			reimbursement=reimbursementService.queryModel(key);
		}
		model.addAttribute("reimbursement",reimbursement);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=saveViewName, method ={ RequestMethod.POST})
	public String save(Model model,Reimbursement reimbursement){
		if(StringUtils.isEmpty(reimbursement.getId())){
			reimbursement.setId(CommonUtils.getKeyId());
			reimbursement.setEmployeeId(getLoginModel().getLoginId());
			if(CommonWebUtils.isTeacher(getLoginModel()))
	    		reimbursement.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
	    	else if(CommonWebUtils.isStudent(getLoginModel()))
	    		reimbursement.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			reimbursement.setModelStatus(ModelStatus.add);
		}else{
			reimbursement.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if(reimbursementService.applyChange(reimbursement)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		model.addAttribute("reimbursement",reimbursement);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=deleteViewName,method= {RequestMethod.POST},produces="text/plain;charset=utf-8")
	public @ResponseBody String delete(String key){
		if(reimbursementService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}